package com.example.demo.mcp.webclient.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zwg
 * @date 2025/10/30 17:41
 */
@Slf4j
@RestController
@RequestMapping("/chat")
@Tag(name = "聊天接口")
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @PostMapping(path = "/message")
    @Operation(summary = "聊天")
    public String message(@RequestBody String message){
        ChatClient.ChatClientRequestSpec reqPrompt = chatClient.prompt();
        reqPrompt.messages(new UserMessage(message));
        String result = "";
        try {

            ChatClient.CallResponseSpec response = reqPrompt.call();
            if (response != null) {
                result = response.content();
            }
        }catch (Exception e) {
            log.error("获取promptType错误",e);
            result = "错误："+e.getMessage();
        }

        return result;
    }
}
